a.$ = function(selector,_dom){
    var events = ['click','change','mouseover','mouseout','mousedown','mousemove','mouseup','keydown','keypress','keyup'];
    function $extend(dom){
        var $ = [dom];
        if(dom){
            $.attr = function(key,value){
                if(value == undefined){
                    return dom.getAttribute(key);
                }else{
                    dom.setAttribute(key,value);
                }
            }
            $.removeAttr = function(key){
                dom.removeAttribute(key);
            }
            var classes = (dom.className || '').split(' ');
            $.classes = {};
            for(var i = 0; i < classes.length; i ++) $.classes[classes[i]] = true;
            function useClasses(){
                var classes = '';
                for(var key in $.classes) if($.classes[key]) classes += ' ' + key;
                dom.className = classes.replace(' ','');
            }
            $.addClass = function(classes){
                classes = classes.split(' ');
                for(var i = 0; i < classes.length; i ++) $.classes[classes[i]] = true;
                useClasses();
            }
            $.removeClass = function(className){
                $.classes[className] = false;
                useClasses();
            }
            $.css = function(_key,value){
                var csses = _key && typeof _key == 'object' ? _key : a.styleSheet(value ? _key + ':' + value : _key);
                for(var key in csses) {
                    dom.style[key] = csses[key];
                    if(key === 'opacity') dom.style.filter = 'alpha(opacity=' + (csses[key] * 100) + ')';
                }
            }
            $.width = function(width){
                if(width == undefined){
                    return a.getBoundingClientRect(dom).width;
                }else{
                    dom.style.width = width + 'px';
                }
            }
            $.height = function(height){
                if(width == undefined){
                    return a.getBoundingClientRect(dom).height;
                }else{
                    dom.style.height = height + 'px';
                }
            }
            $.offset = function(){
                return {
                    left:dom.offsetLeft,
                    top:dom.offsetTop
                }
            }
            $.scrollTop = function(n){
                if(n == undefined){
                    return dom.scrollTop;
                }else{
                    dom.scrollTop = n;
                }
            }
            $.scrollLeft = function(n){
                if(n == undefined){
                    return dom.scrollLeft;
                }else{
                    dom.scrollLeft = n;
                }
            }
            $.size = function(){
                return 1;
            }
            $.hide = function(){
                dom.style.display = 'none';
            }
            $.show = function(){
                dom.style.display = 'block';
            }
            $.parent = function(){
                return dom.parentNode;
            }
            $.prev = function(){
                function prev(dom){
                    if(!dom.previousSibling){
                        return null;
                    }else if(dom.previousSibling.nodeType == 1 || dom.previousSibling.nodeType == 3){
                        return a.$(dom.previousSibling);
                    }else{
                        prev(dom.previousSibling);
                    }
                }
                return prev(dom);
            }
            $.prevAll = function(){
                var prevs = [];
                function prev(dom){
                    if(dom.previousSibling && (dom.previousSibling.nodeType == 1 || dom.previousSibling.nodeType == 3)){
                        prevs.push(dom.previousSibling);
                        if(dom.previousSibling.previousSibling) prev(dom.previousSibling);
                    }
                }
                return $elems(prevs);
            }
            $.next = function(){
                function next(dom){
                    if(!dom.nextSibling){
                        return null;
                    }else if(dom.nextSibling.nodeType == 1 || dom.nextSibling.nodeType == 3){
                        return a.$(dom.nextSibling);
                    }else{
                        next(dom.nextSibling);
                    }
                }
                return next(dom);
            }
            $.nextAll = function(){
                var nexts = [];
                function prev(dom){
                    if(dom.nextSibling && (dom.nextSibling.nodeType == 1 || dom.nextSibling.nodeType == 3)){
                        prevs.push(dom.nextSibling);
                        if(dom.nextSibling.nextSibling) prev(dom.nextSibling);
                    }
                }
                return $elems(nexts);
            }
            $.children = function(){
                return $elems(dom.childNodes);
            }
            $.find = function(selector){
                return a.$(selector,dom);
            }
            $.remove = function(){
                if(dom.parentNode) dom.parentNode.removeChild(dom);
            }
            $.append = function(children){
                if(typeof children == 'string'){
                    children = a.creatElement('div',{innerHTML:children}).childNodes;
                }else{
                    if(!children.sort) children = [children];
                }
                var nodes = [];
                for(var i = 0; i < children.length; i ++) nodes.push(children[i]);
                for(var i = 0; i < nodes.length; i ++){
                    if(nodes[i].nodeType == 1 || nodes[i].nodeType == 3) dom.appendChild(nodes[i]);
                }
            }
            $.html = function(innerHTML){
                if(innerHTML != undefined){
                    dom.innerHTML = '';
                    $.append(innerHTML);
                }else{
                    return dom.innerHTML;
                }
            }
            $.val = function(value){
                if(value == undefined){
                    return dom.value;
                }else{
                    dom.value = value;
                }
            }
            for(var i = 0; i < events.length; i ++){
                (function(eventType){
                    $[eventType] = function(fun){
                        if(typeof fun == 'function'){
                            dom['on' + eventType] = fun;
                        }else{
                            dom[eventType]();
                        }
                    }
                })(events[i])
            }
            $.bind = function(eventType,fun,bool){
                if(dom.addEventListener){
                    dom.addEventListener(eventType,fun,bool);
                }else if(dom.attachEvent){
                    dom.attachEvent('on' + eventType,fun,bool);
                }
            }
            $.unbind = function(eventType,fun){
                if(dom.removeEventListener){
                    dom.removeEventListener(eventType,fun,bool);
                }else if(dom.detachEvent){
                    dom.detachEvent('on' + eventType,fun);
                }
            }
        }else{
            $.size = function(){
                return 0;
            }
        }
        return $;
    }
    function $elems(elems){
        var nodes = [];
        for(var i = 0; i < elems.length; i ++) {
            nodes.push($extend(elems[i]));
        }
        var exts = ['removeAttr','addClass','removeClass','html','width','height','scrollTop','scrollLeft','css','show','hide','remove','append'];
        for(var i = 0; i < exts.length; i ++){
            (function(i){
                nodes[exts[i]] = function(){
                    for(var j = 0; j < nodes.length; j ++) nodes[j][exts[i]].apply(null,arguments);
                }
            })(i)
        }
        nodes.each = function(fun){
            for(var i = 0; i < nodes.length; i ++) (function(i){fun.call(nodes[i][0],i)})(i)
        }
        nodes.eq = function(i){
            return nodes[i];
        }
        nodes.size = function(){
            return nodes.length;
        }
        for(var i = 0; i < events.length; i ++){
            (function(eventType){
                nodes[eventType] = function(fun){
                    for(var i = 0; i < nodes.length; i ++) {
                        nodes[i][eventType](fun);
                    }
                } 
            })(events[i])
        }
        return nodes;
    }
    function $id(id,dom){
        dom = dom && dom.getElementById ? dom : document;
        return $extend(dom.getElementById(id));
    }
    function $tagName(tagName,dom){
        dom = dom && dom.getElementsByTagName ? dom : document;
        return $elems(dom.getElementsByTagName(tagName));
    }
    function $class(className,dom){
        var elems = $tagName('*',dom),rc = [],reg = new RegExp(className + '\\s');
        for(var i = 0; i < elems.length; i ++) if(reg.test(elems[i][0].className + ' ')) rc.push(elems[i][0]);
        return $elems(rc);
    }
    if(typeof selector == 'string'){
        if(selector.charAt(0) == '#'){
            return $id(selector.replace('#',''),_dom);
        }else if(selector.charAt(0) == '.'){
            return $class(selector.replace('.',''),_dom);
        }else{
            return $tagName(selector,_dom);
        }
    }else if(selector.tagName){
        return $extend(selector);
    }else if(selector.sort){
        return $elems(selector);
    }
}